package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.common.ActionEvent;
import com.htc.constants.Constants;
import com.htc.controller.AboutsController;
import com.htc.dlc.fragment.FragmentAboutClup;
import com.htc.dlc.fragment.FragmentAboutGrace;
import com.htc.model.dto.AboutsDTO;

public class AboutModel {
	public static AboutModel instance;

	public static AboutModel getInstance() {
		if (instance == null)
			instance = new AboutModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {
		AboutsDTO about;
		int number;
		switch (e.action) {
		case Constants.GET_ABOUT_CLUP:
			number = 1;
			about = getAllAbout((Object) e.sender, number);
			e.viewData = about;
			AboutsController.getInstance().handleModelViewEvent(e);
			break;
		case Constants.GET_ABOUT_GRACE:
			number = 2;
			about = getAllAbout((Object) e.sender, number);
			e.viewData = about;
			AboutsController.getInstance().handleModelViewEvent(e);
			break;
		default:
			break;
		}
	}

	private AboutsDTO getAllAbout(Object object, int number) {

		AboutsDTO dto = new AboutsDTO();
		String sql = "SELECT * from about_us"
				+ " WHERE about_us_category_number = " + number;
		Context context;
		if (number == 1) {
			FragmentAboutClup club = (FragmentAboutClup) object;
			context = club.getActivity().getBaseContext();
		} else {
			FragmentAboutGrace club = (FragmentAboutGrace) object;
			context = club.getActivity().getBaseContext();
		}

		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {

			dto.set_id(c.getString(c.getColumnIndex("about_us_id")));
			dto.setCategoryTitle(c.getString(c
					.getColumnIndex("about_us_category_title")));
			dto.setCategoryTitleAr(c.getString(c
					.getColumnIndex("about_us_category_title_ar")));
			dto.setCategoryDescription(c.getString(c
					.getColumnIndex("about_us_category_description")));
			dto.setCategoryDescriptioAr(c.getString(c
					.getColumnIndex("about_us_category_description_ar")));
			dto.setAbout_us_category_image(c.getString(c
					.getColumnIndex("about_us_category_image")));
			dto.setCategoryNumber(c.getString(c
					.getColumnIndex("about_us_category_number")));
			dto.setAddedDate(c.getString(c.getColumnIndex("added_date")));
		}
		c.close();
		db.close();
		return dto;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT about_us_id FROM " + Constants.TBL_ABOUT_US
				+ " where about_us_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertAbout(AboutsDTO about, Context context) {
		boolean flag = false;
		if (!checkDB(context, about.get_id())) {
			String DATABASE_TABLE = Constants.TBL_ABOUT_US;
			ContentValues values = new ContentValues();
			values.put("about_us_id", about.get_id());
			values.put("about_us_category_title", about.getCategoryTitle());
			values.put("about_us_category_description",
					about.getCategoryDescription());
			values.put("about_us_category_title_ar", about.getCategoryTitleAr());
			values.put("about_us_category_description_ar",
					about.getCategoryDescriptionAr());
			values.put("about_us_category_image",
					about.getAbout_us_category_image());
			values.put("added_date", about.getAddedDate());
			values.put("about_us_category_number", about.getCategoryNumber());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateAbout(AboutsDTO about, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_ABOUT_US;
		String where = "about_us_id='" + about.get_id() + "'";
		ContentValues values = new ContentValues();

		values.put("about_us_id", about.get_id());
		values.put("about_us_category_title", about.getCategoryTitle());
		values.put("about_us_category_description",
				about.getCategoryDescription());
		values.put("about_us_category_title_ar", about.getCategoryTitleAr());
		values.put("about_us_category_description_ar",
				about.getCategoryDescriptionAr());
		values.put("about_us_category_image",
				about.getAbout_us_category_image());
		values.put("added_date", about.getAddedDate());
		values.put("about_us_category_number", about.getCategoryNumber());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteAbout(AboutsDTO about, Context context) {
		String sql = "about_us_id='" + about.get_id() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_ABOUT_US;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
